/*
 * @Description:   
 * @Author: YangJianFei
 * @Date: 2024-10-09 10:55:23
 * @LastEditTime: 2024-10-12 18:00:14
 * @LastEditors: YangJianFei
 * @FilePath: \yt_devop_app\lib\pages\devices\device_controll\component\rate_setting\rate_setting_model.dart
 */

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/yt_table/yt_column_model.dart';
import 'package:yt_devops/components/yt_table/yt_profile_table.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';

class TimeZoneData extends YtProfileTableData {
  int index;
  int? Tn;
  String? Md;

  TimeZoneData({required this.index, this.Tn, this.Md});

  @override
  Map<String, dynamic> toJson([bool noIndex = false]) {
    return noIndex
        ? {
            'Tn': Tn,
            'Md': Md,
          }
        : {
            'index': index,
            'Tn': Tn,
            'Md': Md,
          };
  }
}

class TimePeriodData extends YtProfileTableData {
  int index;
  int? m37;
  String? Hm;

  TimePeriodData({required this.index, this.m37, this.Hm});

  @override
  Map<String, dynamic> toJson([bool noIndex = false]) {
    return noIndex
        ? {
            'm37': m37,
            'Hm': Hm,
          }
        : {
            'index': index,
            'm37': m37,
            'Hm': Hm,
          };
  }
}

class RateSettingModel {
  static List<YtColumnModel<TimeZoneData>> timeZone = [
    YtColumnModel(
      title: FuncUtils.formatMessage(
          resKey: ResKey.serialNumber, defaultMessage: '序号'),
      field: 'index',
    ),
    YtColumnModel(
      title: FuncUtils.formatMessage(
          resKey: ResKey.timePeriodTable, defaultMessage: '时段表'),
      field: 'Tn',
      render: (item, index) {
        return Text(
          item.Tn != null
              ? '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${item.Tn}'
              : '-',
          style: TextStyles.f14,
        );
      },
    ),
    YtColumnModel(
      title: FuncUtils.formatMessage(resKey: ResKey.date, defaultMessage: '日期'),
      field: 'Md',
    )
  ];

  static List<YtColumnModel<TimeZoneData>> getTimeZoneForm(
    Function(String type, int rowIndex) selectColClick,
    Function(String type, int rowIndex) timeColClick,
  ) {
    List<YtColumnModel<TimeZoneData>> timeZoneForm = [
      YtColumnModel(
        title: FuncUtils.formatMessage(
            resKey: ResKey.serialNumber, defaultMessage: '序号'),
        field: 'index',
      ),
      YtColumnModel(
        width: 130,
        title: FuncUtils.formatMessage(
            resKey: ResKey.timePeriodTable, defaultMessage: '时段表'),
        field: 'Tn',
        render: (item, index) {
          return Center(
            child: GestureDetector(
              child: Container(
                width: Get.locale?.languageCode == 'zh' ? 90 : 130,
                height: 36,
                padding: EdgeInsets.fromLTRB(16, 6, 16, 6),
                decoration: BoxDecoration(
                  color: Colour.backgroundColor,
                  borderRadius: BorderRadius.circular(5),
                ),
                alignment: Alignment.center,
                child: Text(
                  item.Tn != null
                      ? '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${item.Tn}'
                      : '-',
                  style: TextStyles.f14,
                ),
              ),
              onTap: () {
                selectColClick('timePeriod', index);
              },
            ),
          );
        },
      ),
      YtColumnModel(
        title:
            FuncUtils.formatMessage(resKey: ResKey.date, defaultMessage: '日期'),
        field: 'Md',
        render: (item, index) {
          return index > 0
              ? Center(
                  child: GestureDetector(
                    child: Container(
                      width: 80,
                      height: 36,
                      padding: EdgeInsets.fromLTRB(16, 6, 16, 6),
                      decoration: BoxDecoration(
                        color: Colour.backgroundColor,
                        borderRadius: BorderRadius.circular(5),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        item.Md ?? '-',
                        style: TextStyles.f14,
                      ),
                    ),
                    onTap: () {
                      timeColClick('date', index);
                    },
                  ),
                )
              : Text(
                  item.Md ?? '-',
                  style: TextStyles.f14,
                );
        },
      )
    ];

    return timeZoneForm;
  }

  static List<YtColumnModel<TimePeriodData>> timePeriod = [
    YtColumnModel(
      title: FuncUtils.formatMessage(
          resKey: ResKey.serialNumber, defaultMessage: '序号'),
      field: 'index',
    ),
    YtColumnModel(
      title: FuncUtils.formatMessage(resKey: ResKey.rate, defaultMessage: '费率'),
      field: 'm37',
      render: (item, index) {
        return Text(
          item.m37 != null ? 'T${item.m37}' : '-',
          style: TextStyles.f14,
        );
      },
    ),
    YtColumnModel(
      title: FuncUtils.formatMessage(resKey: ResKey.time, defaultMessage: '时间'),
      field: 'Hm',
    )
  ];

  static List<YtColumnModel<TimePeriodData>> getTimePeriodForm(
    Function(String type, int rowIndex) selectColClick,
    Function(String type, int rowIndex) timeColClick,
  ) {
    List<YtColumnModel<TimePeriodData>> timePeriodForm = [
      YtColumnModel(
        title: FuncUtils.formatMessage(
            resKey: ResKey.serialNumber, defaultMessage: '序号'),
        field: 'index',
      ),
      YtColumnModel(
        title:
            FuncUtils.formatMessage(resKey: ResKey.rate, defaultMessage: '费率'),
        field: 'm37',
        render: (item, index) {
          return Center(
            child: GestureDetector(
              child: Container(
                width: 90,
                height: 36,
                padding: EdgeInsets.fromLTRB(16, 6, 16, 6),
                decoration: BoxDecoration(
                  color: Colour.backgroundColor,
                  borderRadius: BorderRadius.circular(5),
                ),
                alignment: Alignment.center,
                child: Text(
                  item.m37 != null ? 'T${item.m37}' : '-',
                  style: TextStyles.f14,
                ),
              ),
              onTap: () {
                selectColClick('rate', index);
              },
            ),
          );
        },
      ),
      YtColumnModel(
        title:
            FuncUtils.formatMessage(resKey: ResKey.time, defaultMessage: '时间'),
        field: 'Hm',
        render: (item, index) {
          return index > 0
              ? Center(
                  child: GestureDetector(
                    child: Container(
                      width: 80,
                      height: 36,
                      padding: EdgeInsets.fromLTRB(16, 6, 16, 6),
                      decoration: BoxDecoration(
                        color: Colour.backgroundColor,
                        borderRadius: BorderRadius.circular(5),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        item.Hm ?? '-',
                        style: TextStyles.f14,
                      ),
                    ),
                    onTap: () {
                      timeColClick('time', index);
                    },
                  ),
                )
              : Text(
                  item.Hm ?? '-',
                  style: TextStyles.f14,
                );
        },
      )
    ];

    return timePeriodForm;
  }
}
